<template>
	<view class="tab-content">
		<view class=" uni-common-mt">
			<uni-segmented-control :current="current" :values="tabBars" style-type="button"
				active-color="#18bc9c" @clickItem="onClickItem" />
		</view>

		<view v-show="current === 0">
			<tableHead :poorder="poorder" prestr="OUT" :order_type="order_type" ref="thead"></tableHead>
		</view>

		<view v-show="current === 1">
			<scanInvShelves :getkcFlag="true" :realQuantityZeroFlag="false" :kcQuantityZeroFlag="false" :toSuccessTip="false" :order_type="order_type" ref="scanIS"></scanInvShelves>
			<view class="swiper-button">
				<button @click="checkOrder">完成</button>
				<button @click="$refs.scanIS.toAdd()" >添加</button>
			</view>
		</view>
		
		<view v-show="current === 2">
			<scroll-view scroll-y="true" style="height: 80vh;">
				<view class="form_list">
					<showMaterialList :dataArr="porderItem" :orderType="3" :showShelves="showShelves" :showShelvesName="showShelvesName" ref="showMaterialList" flag='out' showTip="请扫物料二维码出库"></showMaterialList>
				</view>
			</scroll-view>
			
			<view class="swiper-button">
				<button @click="toReset">重置</button>
				<!-- <button @click="$refs.thead.toSerial()">选择源单</button> -->
				<button @click="checkOrder">完成</button>
			</view>
		</view>
		
	</view>
</template> 

<script>
	import tableHead from "@/component/table-head.vue"
	import scanInvShelves from "@/component/scan-inv-shelves.vue"
	import showMaterialList from "@/component/show-material-list.vue"
	import {
		uniSegmentedControl
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniSegmentedControl,tableHead,showMaterialList,scanInvShelves
		},
		data() {
			return {
				current: 1,
				tabBars: ['表头','扫描', '表体'],
				
				// 保存的数据对象
				poorder: {}, //源单对象
				porderItem: [],//动态变化的数组
				order_type:18,//直接出库
				
				user:uni.getStorageSync('user'),
				
				showShelves:false,
				showShelvesName:true //默认显示货架，没得选
			}
		},
		computed: {},
		watch:{},
		onLoad(options) { 
			this.$Func.setBarTitle('直接领料出库')
			//初始化
			setTimeout(()=>{
				this.$refs.thead.init() //初始化
			},500)
			
			/* 监听传过来的退回明细 */
			uni.$off("checkItems")
			uni.$on("checkItems", (value) => {
				if(JSON.stringify(this.poorder) == '{}'){
					this.porderItem = value;
				}else{
					this.$Api.msg('请先重置单据后再扫码领料出库')
					this.$refs.scanIS.reset(true)//表体数组也一起清除
				}
			})
		},
		onShow() {
			//监听选择源单
			uni.$off('getPick')
			uni.$once("getPick", (value) => {
				this.poorder = value
				
				this.order_type = 3
				this.showShelves = false
				this.showShelvesName = true //显示货架，没得选
				
				this.poorder.cdepname = value.factory_name //从本地单据的字段转换
				this.$refs.thead.remark = value.a_remark
				
				uni.showLoading();
				//获得表体记录
				this.$Db.selectTableData('stockbillentry', 'FBillNo', value.FBillNo).then(res => {
					this.porderItem = []//先清空之前
					
					res.forEach((arr) => {
						arr.real_quantity = this.$Func.decimalPattern(arr.fqty - arr.fauxqty)
						arr.quantity = arr.real_quantity //对应show-material-list组件的字段
						arr.unit_name = arr.inv_unit //对应show-material-list组件的字段
						arr.b_remark = value.source_code //这里的表体备注，用来保存PC端领料单的编码，作为源单
						if(arr.real_quantity>0&&arr.status){
							arr.shelves_id = value.to_shelves_id //暂存区的位置
							arr.shelves_name = value.to_shelves_name
							this.porderItem.push(arr)
						}
					});
					uni.hideLoading();
				})
			})
		},
		methods: {
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			//核实物料
			async checkOrder() {
				let _this = this
				let check = []
				let check_total = 0 //总数不能为0
				let billentry = []
				
				if(!this.porderItem.length){
					this.$Api.msg('请扫物料二维码','error');
					return;
				}
			
				for(let i in this.porderItem){
					let citem = this.porderItem[i]
					if( isNaN(citem.real_quantity) || citem.real_quantity == 0){
						continue
					}
					
					check_total += parseFloat(citem.real_quantity)//累加总数
					citem.quantity = this.$Func.decimalPattern(citem.quantity - citem.commit_quantity)
					
					//实收数量超过应收提示,有源单情况
					if(JSON.stringify(this.poorder) != '{}'){
						if(citem.real_quantity>citem.quantity){
							this.$Api.msg(citem.inv_name+'出库数量超出');
							return ;
						}
					}else{
						//没源单情况的领料
						citem.row_id = 0
					}
					
					if(citem.shelves_id == 0 || isNaN(citem.shelves_id)){
						this.$Api.msg(citem.inv_name+",请选择库区")
						return 
					}
					
					//单据表体添加
					billentry.push({
						//'source_id':citem.id,//拣货表体id
						'FBillNo':this.$refs.thead.FBillNoPda,
						'auto_id':this.poorder.auto_id==undefined?0:this.poorder.auto_id,
						'row_id':citem.row_id,
						'local_row_id':this.poorder.auto_id==undefined?i:citem.row_id,
						'inv_number':citem.inv_number,
						'inv_name':citem.inv_name,
						'inv_specification':citem.inv_specification,
						'inv_attributes':citem.inv_attributes,
						'inv_unit':citem.inv_unit,
						'inv_unit_code':citem.inv_unit_code,
						'fqty':citem.real_quantity,
						'fauxqty':citem.real_quantity,
						'cBatch':citem.cBatch,
						'shelves_id':citem.shelves_id,
						'shelves_name':citem.shelves_name,
						'shelves_type':citem.shelves_type,
						'b_remark':this.order_type==3?citem.b_remark:'' //源单出库，这里记录领料申请单编码
					})
				}
				
				if(!check_total||!billentry.length||isNaN(check_total)){
					this.$Api.msg("出库数量全为0",'error')
					return 
				}
				
				check_total = this.$Func.decimalPattern(check_total)
				
				let bill = {//准备表头数据
					'auto_id':this.order_type==18?0:this.poorder.auto_id,
					'source_code':this.order_type==18?'':this.poorder.FBillNo,
					'order_type':this.order_type,//3领料出库 18直接出库
					'FBillNo':this.$refs.thead.FBillNoPda,
					'FDate':this.$refs.thead.FDateVal,
					'a_remark':this.$refs.thead.remark,
					'stock_id':this.$refs.thead.stock_id,
					'status':1,
					'syn_status':0,
					'total_qty':check_total,
					'factory_code':this.order_type==18?'':this.poorder.cdepcode,
					'factory_name':this.order_type==18?'':this.poorder.cdepname,
					'userid':this.user.userid,
					'create_date':this.$Func.getDateTime('today','Y-m-d')
				}
				this.addOrder(bill,billentry)
			},
			//添加本地单据
			async addOrder(bill,billentry){
				uni.showLoading({mask:true})
				
				let order_type = 3
				if(JSON.stringify(this.poorder) == '{}'){
					order_type = 18
				}
				
				this.$Db.addOrder(order_type,bill,billentry).then(res=>{
					this.$Api.msg('添加成功','success')
					this.reset()
					uni.hideLoading();
				}).catch(err=>{
					this.$Api.msg('添加失败','error')
					this.$Db.addlog('添加领料出库',err)
					uni.hideLoading();
				})
			},
			toReset(){
				let _this = this
				uni.showModal({
				    title: '确定要重置单据？',
				    //content: '确定要删除记录？',
				    success: function (res) {
				        if (res.confirm) {
							_this.reset()
						}
					},
				})
			},
			//重置表单
			reset() {
				this.porderItem = [];
				this.$set(this.poorder, 'FBillNo', ''); // 清空对象属性
				this.$set(this.poorder, 'cdepname', ''); // 清空对象属性
				this.$refs.thead.reset() //初始化
				this.$refs.scanIS.reset(true)//表体数组也一起清除
				this.order_type = 18
				setTimeout(()=>{
					this.poorder = {};
				},500)
			},
		}
	}
</script>
<style lang="scss">
</style>